package genedit.model.graph.classModel;

import genedit.gui.GMainFrame;
import genedit.gui.exceptions.ElementNePostoji;
import genedit.gui.exceptions.ElementPostoji;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

public class GMethods implements ITableModel,Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1091031612742477776L;
	private ArrayList<GMethod> methods = new ArrayList<GMethod>();
	private ArrayList<GMethod> inheritedMethods = new ArrayList<GMethod>();
	
	public void addMethod(GMethod novi) throws ElementPostoji {
		if(methods.contains(novi))
			throw new ElementPostoji(GMainFrame.getInstance().getResourceBundle().getString("PostojiA"));
		methods.add(novi);
	}
	
	public void change(GMethod novi)  {
		methods.remove(novi);	
		methods.add(novi);
	}
	
	public void changeMethod(GMethod novi) throws ElementNePostoji {
		if (!methods.contains(novi))
			throw new ElementNePostoji(GMainFrame.getInstance().getResourceBundle().getString("NePostojiA"));
		methods.add(novi);
	}
	
	public void removeMethod(GMethod stari) throws ElementNePostoji {
		if (!methods.contains(stari))
			throw new ElementNePostoji(GMainFrame.getInstance().getResourceBundle().getString("NePostojiA"));
		methods.remove(stari);
	}
	
	public GMethod getMethod(String key) throws ElementNePostoji {
		for(GMethod method : methods)
		if (method.getName().equals(key))
			return method;
		throw new ElementNePostoji(GMainFrame.getInstance().getResourceBundle().getString("NePostojiA"));
	}
	
	public GMethod getMethodbyName(String method) {
		for(int i = 0; i < methods.size(); i++)
			if(methods.get(i).getName().equals(method))
				return methods.get(i);
		return null;
	}
	
	public Iterator<String> getMethodIterator() {
		ArrayList<String> names = new ArrayList<String>();
		for(GMethod method : methods)
			names.add(method.getName());
		return names.iterator();
	}
	public boolean containsMethod(GMethod method) {
		for(int i = 0; i < methods.size(); i++)
			if(methods.get(i).getName().equals(method.getName()) && methods.get(i).getReturnType().equals(method.getReturnType()))
				return true;
		return false;
	}
	
	public ArrayList<String> getMethodKeys() {
		ArrayList<String> names = new ArrayList<String>();
		for(GMethod method : methods)
			names.add(method.getName());
		return names;
	}
	
	public ArrayList<GMethod> getMethods() {
		return methods;
	}

	public ArrayList<GMethod> getInheritedMethods() {
		return inheritedMethods;
	}

	public void setInheritedMethods(ArrayList<GMethod> inheritedMethods) {
		this.inheritedMethods = inheritedMethods;
	}

	@Override
	public int getRowCount() {
		return methods.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		switch(columnIndex){
			case 0: return methods.get(rowIndex).getVisibility();
			case 1: return methods.get(rowIndex).getReturnType();
			case 2: return methods.get(rowIndex).getName();
			case 3: return methods.get(rowIndex).getStringParameters();
		}
		return "";
	}
	
}
